package com.example.lin.generate;

import com.example.lin.common.BaseExcel;
import com.example.lin.util.ExportUtil;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * @className: ReadExcelOutExcel
 * @description: 读取Excel数据并输出Excel
 * @author: Lin.Gu
 * @date: 2025/5/27
 */
public class ReadExcelOutExcel {

    public static Set<String> checkSet = new HashSet<>();

    public static void main(String[] args) throws Exception {
        //明细
        redExcel("C:\\Users\\181596\\Desktop\\重call数据整理已去除重复SKU.xlsx", 0);
        //主表
        redExcel("C:\\Users\\181596\\Desktop\\重call数据整理已去除重复SKU.xlsx", 1);
    }

    public static void redExcel(String filePath, int sheet) throws Exception {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new Exception("文件不存在!");
        }
        InputStream in = new FileInputStream(file);
        // 读取整个Excel
        XSSFWorkbook sheets = new XSSFWorkbook(in);
        // 获取第一个表单Sheet
        XSSFSheet sheetAt = sheets.getSheetAt(sheet);

        List<Object[]> data = new ArrayList<>();
        data.add(new Object[]{"itemId", "variation sku"});


        for (int i = 1; i < sheetAt.getPhysicalNumberOfRows(); i++) {
            XSSFRow row = sheetAt.getRow(i);
            if (Objects.isNull(row)) {
                break;
            }
            String itemId = BaseExcel.getString(row.getCell(0));
            String variationSku = BaseExcel.getString(row.getCell(1));

            String key = itemId + variationSku;

            if(checkSet.contains(key)){
                System.out.println("重复数据信息:" + key);
                continue;
            }
            checkSet.add(key);
            data.add(new Object[]{itemId, variationSku});
        }

        //导出数据
        if (data.size() > 1) {
            System.out.println("总条数信息:" + data.size());
            ExportUtil.exportTheFile("重call数据整理已去除重复SKU", data);
        }

    }
}
